package com.dingpeikeji.quickdeer.modules.rentproduct.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.modules.sys.dao.OfficeDao;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TRentprodPriceDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TRentgroupAuditDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TRentproductDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentprodPrice;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentgroupAudit;

/**
 * 发布整车Service
 * @author wuhao
 * @version 2016-08-08
 */
@Service
@Transactional(readOnly = true)
public class TRentproductService extends CrudService<TRentproductDao, TRentproduct> {
	@Autowired
	private OfficeDao officeDao;
	@Autowired
	private TRentprodPriceDao priceDao;
	@Autowired
	private TRentgroupAuditDao rentgroupAuditDao;

	public TRentproduct get(Integer rentid) {
		return dao.get(rentid);
	}
	
	public List<TRentproduct> findList(TRentproduct tRentproduct) {
		if(tRentproduct.getOffice() == null){
			User user = UserUtils.getUser();
			// 设置部门
			Office office = user.getOffice();
			tRentproduct.setOffice(office);
		}
		return super.findList(tRentproduct);
	}
	
	public List<TRentproduct> findProductList(TRentproduct tRentproduct) {
		return dao.findProductList(tRentproduct);
	}
	
	public Page<TRentproduct> findPage(Page<TRentproduct> page, TRentproduct tRentproduct) {
		if(tRentproduct.getOffice() == null){
			User user = UserUtils.getUser();
			// 设置部门
			Office office = user.getOffice();
			tRentproduct.setOffice(office);
		}
		return super.findPage(page, tRentproduct);
	}
	
	@Transactional(readOnly = false)
	public void save(TRentproduct tRentproduct) {
		if(tRentproduct.getRentid()==null){
			
			// 操作人
			
			User user = UserUtils.getUser();
			tRentproduct.setCreateBy(user);
			tRentproduct.setCreateDate(new Date());
			
			// 设置部门
			
			Office office = user.getOffice();
			tRentproduct.setOffice(office);
			
			// 新建时是待审核
			
			tRentproduct.setAuditstatus(TRentgroupAudit.AUDITSTATUS_WAIT);
			
			dao.insert(tRentproduct);
			
		
/*			// 生成默认的价格区间
			
			TRentprodPrice price = new TRentprodPrice(); // 价格区间
			price.setRentid(tRentproduct.getRentid());
			price.setSuitrtnprice(tRentproduct.getSuitrtnprice());
			price.setSuitonewayprice(tRentproduct.getSuitonewayprice());
			price.setSuitfreeprice(tRentproduct.getSuitfreeprice());
			price.setEfftdate(tRentproduct.getFromdate());
			price.setInvaliddate(tRentproduct.getStopdate());
			priceDao.insert(price);*/
		} else {
			// 不修改整车发布
			// dao.update(tRentproduct);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(TRentproduct tRentproduct) {
		super.delete(tRentproduct);
	}
	
	// 获取旅行商和电商列表
	public List<Office> findOfficeList() {
		Office office =new Office();
		
		// TODO 暂时通过两条查询语句获取旅行商和电商列表
		
		office.setFlatType(Office.FLAT_TYPE_ENTERPRISE);
		List<Office> list = officeDao.findList(office);
		office.setFlatType(Office.FLAT_TYPE_EBUSINESS);
		list.addAll(officeDao.findList(office));
		return list;
	}


}